package com.sy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sy.dto.HomeworkMagTable;
import com.sy.dto.HomeworkView;
import com.sy.entity.*;
import com.sy.exception.BusinessException;
import com.sy.mapper.HomeworkMapper;
import com.sy.service.HomeworkService;
import com.sy.vo.ChoiceVo;
import com.sy.vo.HomeworkVo;
import com.sy.vo.QuestionsVo;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class HomeworkServiceImpl extends BaseServiceImpl implements HomeworkService {
    @Override
    @Transactional(rollbackFor = {BusinessException.class})
    public void addHomework(HomeworkVo homeworkVo, Integer teacherId) {
        try {
            //根据作业标题和老师id查找作业
            Integer hasHomeworkId = homeworkMapper.getHomeworkIdByTitle(homeworkVo, teacherId);
            if (!Objects.isNull(hasHomeworkId)) {
                throw new BusinessException("该作业标题已存在");
            }
            //添加作业
            homeworkMapper.addHomework(homeworkVo, teacherId);
//        根据作业标题查找作业的id
            Integer homeworkId = homeworkMapper.getHomeworkIdByTitle(homeworkVo, teacherId);
            //根据作业id添加简答题
            homeworkMapper.addShortQuestion(homeworkVo, homeworkId);
            //当前作业的选择题列表
            List<QuestionsVo> questions = homeworkVo.getQuestions();
            for (QuestionsVo questionsVo : questions) {
                //添加选择题
                homeworkMapper.addChoiceQuestions(questionsVo, homeworkId);
                //查询添加选择题的id
                Integer choiceQuestionId = homeworkMapper.getChoiceQuestionIdByQuestion(questionsVo);
                //获得选择题下的选择项集合
                List<ChoiceVo> choices = questionsVo.getChoices();
                for (ChoiceVo choiceVo : choices) {
                    //根据选择题id添加对应的选项
                    homeworkMapper.addChoices(choiceVo, choiceQuestionId);
                }
                //根据答案选项的内容获得对用选项的id并写入答案表
                Integer answerChoiceId = homeworkMapper.getAnswerChoiceIdByAnswer(questionsVo);
                homeworkMapper.addChoiceAnswer(homeworkId, choiceQuestionId, answerChoiceId);
            }
        }catch(Exception e){
            throw new BusinessException(HttpStatus.NOT_FOUND,e.getMessage());
        }

    }

    @Override
    public Page<HomeworkMagTable> getHomeworkListByTeacherId(Integer teacherId, Integer page, Integer limit, String keyWords) {
        PageHelper.startPage(page, limit);
        Page<HomeworkMagTable> homeworkList = (Page<HomeworkMagTable>) homeworkMapper.getHomeworkListByTeacherId(teacherId, keyWords);
        return homeworkList;
    }


    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED
            , rollbackFor = {BusinessException.class})
    public boolean deleteHomework(Integer homeworkId) {
        //删除作业表中的作业
        boolean deleteHomework = homeworkMapper.deleteHomework(homeworkId);
        //根据作业id获得简答题的id
        Integer shortQuestionId = homeworkMapper.getShortQuestionIdByHomeworkId(homeworkId);
        //根据作业id删除简答题
        boolean deleteShortQuestion = homeworkMapper.deleteShortQuestion(homeworkId);
        //根据简答题id查找简答题提交表
        boolean deleteShortQuestionSubmit=true;
        List<SubmitShortAnswer> submitShortAnswerList = homeworkMapper.getSubmitShortAnswerByShortQuestionId(shortQuestionId);
        if (!submitShortAnswerList.isEmpty()) {
            //不为空
            //根据作业id删除已提交的简答题答案
        deleteShortQuestionSubmit = homeworkMapper.deleteShortQuestionSubmitById(shortQuestionId);
        }
        //根据作业id删除选择题标准答案
        boolean deleteChoiceAnswer = homeworkMapper.deleteChoiceAnswer(homeworkId);
        //根据作业id获取已提交的选择题答案
        boolean deleteChoiceAnswerSubmit=true;
        List<SubmitChoiceAnswer> submitChoiceAnswerList = homeworkMapper.getSubmitChoiceAnswerByHomeworkId(homeworkId);
        if (!submitChoiceAnswerList.isEmpty()) {
            //不为空
            //根据作业id删除已提交的选择题答案
            deleteChoiceAnswerSubmit = homeworkMapper.deleteChoiceAnswerSubmit(homeworkId);
        }
        //根据作业id查找分数表
        boolean deleteScore=true;
        List<Score> scoreList = homeworkMapper.getScoreByHomeworkId(homeworkId);
        if (!scoreList.isEmpty()) {
            //不为空
            //根据作业id删除分数表
             deleteScore = homeworkMapper.deleteScoreByHomeworkId(homeworkId);
        }
        boolean result = deleteHomework && deleteShortQuestion && deleteShortQuestionSubmit && deleteChoiceAnswer && deleteChoiceAnswerSubmit && deleteScore;
        if (!result) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "删除失败");
        }
        return true;

    }

    @Override
    public HomeworkView viewHomework(Integer homeworkId) {
        HomeworkView homeworkView = new HomeworkView();
        //获取作业信息
        Homework homework = homeworkMapper.getHomeworkById(homeworkId);
        homeworkView.setHomework(homework);
        //获取简答题信息
        ShortQuestion shortQuestion = homeworkMapper.getShortQuestionByHomeworkId(homeworkId);
        homeworkView.setShortQuestion(shortQuestion);
        //获取选择题信息
        List<ChoiceQuestion> choicesQuestionList = homeworkMapper.getChoicesQuestionByHomeworkId(homeworkId);
        for (ChoiceQuestion choiceQuestion :choicesQuestionList ) {
            Integer questionId = choiceQuestion.getId();
            List<Choice> choices = homeworkMapper.getChoicesByQuestionId(questionId);
            choiceQuestion.setChoices(choices);
        }
        homeworkView.setQuestions(choicesQuestionList);
        return  homeworkView;
    }

    @Override
    public List<Answer> getAnswer(Integer homeworkId) {
        return homeworkMapper.getAnswer(homeworkId);
    }


}
